var App = App || {};

App.live = function() {
      /*LIVE DATA CHART*/
      
    var data   = [], totalPoints = 100; 
    
    /**
    function getRandomData() {
        if (data.length > 0) data = data.slice(1);
        // Do a random walk
        while (data.length < totalPoints) {
          var prev = data.length > 0 ? data[data.length - 1] : 50,
          y = prev + Math.random() * 10 - 5;
          if (y < 0) {
            y = 0;
          } else if (y > 100) {
            y = 100;
          }
          data.push(y);
        }
        // Zip the generated y values with the x values
        var res = [];
        for (var i = 0; i < data.length; ++i) {
         res.push([i, data[i]])
        }
        console.log(res);
        return res;
    } 
    **/

    var sdata  = [];
    function getJMLiveData() {
        //console.log(sdata);

        if (data.length > 0) data = data.slice(1);
        // Do a random walk
        while (data.length < totalPoints) {
          //var prev = data.length > 0 ? data[data.length - 1] : 50,
          y = sdata.shift(1);
          //console.log(y);
          if (y < 0) {
            y = 0;
          }
          else if (y > 100) {
            y = 100;
          }
          //y += 30;
          data.push(y);
        }
        // Zip the generated y values with the x values
        var res = [];
        for (var i = 0; i < data.length; ++i) {
         res.push([i, data[i]])
        }
        //console.log(res);
        return res;
    }

    function update2() {
        chart4.setData([getJMLiveData()]);
        // Since the axes don't change, we don't need to call plot.setupGrid()
        chart4.setupGrid();
        chart4.draw();
        setTimeout(update2, 3500);

        if (sdata.length < 10) {
          axios.get('stat/circulates4live?now=1').then(function(res){

            var total = parseInt(res.data.data.data);
            if (!isNaN(total)) {
              sdata.push(total);
            }

          });
        }
    }

    /**
    function update() {
        init_draw();

        chart4.setData([getRandomData()]);
        // Since the axes don't change, we don't need to call plot.setupGrid()
        chart4.draw();
        setTimeout(update, 3500);
    }**/

    var chart4;
    function init_draw() {
      chart4 = $.plot($("#chart4"), [{
        //data:  getRandomData() ,
        data: getJMLiveData(),
        label: "动态借还量"
       }
      ], {
        series: {
          lines: {
            show: true,
            lineWidth: 2, 
            fill: true,
            fillColor: {
              colors: [{
                opacity: 0.25
              }, {
                opacity: 0.25
              }
              ]
            } 
          },
          points: {
            show: false
          },
          shadowSize: 2
        },
        legend:{
          show: true
        },
        grid: {
          labelMargin: 10,
          axisMargin: 500,
          hoverable: true,
          clickable: true,
          tickColor: "rgba(0,0,0,0.15)",
          borderWidth: 0
        },
        colors: ["#B450B2", "#4A8CF7", "#52e136"],
        xaxis: {
          ticks: 11,
          tickDecimals: 0
        },
        yaxis: {
          ticks: 5,
          tickDecimals: 0
        }
      });
    }
      
    var up_draw2 = function() {
        axios.get('/stat/circulates4live').then(function(res){
          res.data.data.data.forEach(function(item) {
            sdata.push(item.TOTAL);
          });
          init_draw();

          setTimeout(update2, 2000);
        });
    };
    up_draw2();

    //update();
}

App.live_books = function (){
  var update_book = function () {
    axios.get('/stat/loans4latest').then(function(res){
        //console.log(res);
        var num = 0;
        var all = 0;
        var table = '';
        res.data.data.data.forEach(function(v) {
          //if (++num>22)  return;
          table += "<tr><td>" 
            + v.RDNAME + "</td><td>" 
            + v.TITLE + "</td><td>" 
            + v.AUTHOR + "</td><td class='number'>"
            + v.DUETIME +"</td></tr>";

            //all += v.TOTAL;
        }) ;
        $("#newbooks-table tbody").html(table);
    });
    setTimeout(update_book, 60*1000);
  }
  update_book();

  var updata_total = function() {
    axios.get('/stat/summary4today').then(function(res){
      //console.log(res.data.data.CIRCULATES);
      $('#data-circuate-total .value').data("end", res.data.data.CIRCULATES);
      $('#data-booking-total .value').data("end",res.data.data.BOOKINGS);
      counter();
      setTimeout(updata_total, 20000);
    });
  }
  updata_total();

  var counter = function() {
     $('[data-toggle="counter"]').each(function(i, e){
        var _el       = $(this);
        var prefix    = '';
        var suffix    = '';
        var start     = 0;
        var end       = 0;
        var decimals  = 0;
        var duration  = 2.5;

        if( _el.data('prefix') ){ prefix = _el.data('prefix'); }

        if( _el.data('suffix') ){ suffix = _el.data('suffix'); }

        if( _el.data('start') ){ start = _el.data('start'); }

        if( _el.data('end') ){ end = _el.data('end'); }

        if( _el.data('decimals') ){ decimals = _el.data('decimals'); }

        if( _el.data('duration') ){ duration = _el.data('duration'); }

        var count = new CountUp(_el.get(0), start, end, decimals, duration, { 
          suffix: suffix,
          prefix: prefix,
        });

        count.start();
      });
  }

}
      